<?php
/**
 * @author: <136327134@qq.com>
 * @day: 2021/6/4
 */

namespace App\Services\system;

use App\Services\BaseServices;
use App\Dao\system\SystemAdminDao;
use App\Exceptions\ApiException as HOException;
use Hlcreation\Basic\Helper;
use App\Services\system\SystemMenusServices;
use Hlcreation\Services\CacheServices;
use App\Services\system\SystemLogServices;


/**
 * 管理员service
 */
class SystemAdminServices extends BaseServices
{


    /**
     * SystemAdminServices constructor.
     * @param SystemAdminDao $dao
     */
    public function __construct(SystemAdminDao $dao)
    {

        $this->dao = $dao;

    }

    /**
     * 管理员登陆
     * @param string $account
     * @param string $password

     */
    public function verifyLogin(string $account, string $password)
    {

        $adminInfo = $this->dao->accountByAdmin($account);

        if (!$adminInfo) throw new HOException("账号不存在!");

        if ($adminInfo->it_disabled) throw new HOException('您已被禁止登录!');

        if($adminInfo->pwd!=Helper::returnPwd($password, $adminInfo->halt)) {
            throw new HOException('账号或密码错误，请重新输入');
        }

        if($adminInfo->expiration_time!='永久'){

            if($adminInfo->expiration_time<time()) throw new HOException('账号已过期!');
        }

        $adminInfo->last_ip = app('request')->ip();
        $adminInfo->login_count=$adminInfo->login_count+1;
        $adminInfo->save();
        return $adminInfo;
    }

    /**
     * 后台登陆获取菜单获取token
     * @param string $account
     * @param string $password
     * @return array
     */
    public function login(string $account, string $password)
    {

        $adminInfo = $this->verifyLogin($account, $password);
        $tokenInfo = Helper::returnToken($adminInfo['id']);
        /** @var SystemMenusServices $services */

        $services = app()->make(SystemMenusServices::class);

        $systme_menus= $services->getMenusList($adminInfo);
        CacheServices::set($tokenInfo, json_encode(
            ['token'=>$tokenInfo,'user_info'=>['id'=>$adminInfo->id,'account'=>$adminInfo->account,'level'=>$adminInfo->level,'store_num'=>$adminInfo->store_num,'sms_num'=>$adminInfo->sms_num,'shop_num' => $adminInfo->shop_num]]


        ));
        //临时使用短信警告
        if($adminInfo->sms_num<100) $warning=true;
        else $warning=false;
        //插入日志
        $LogServices = app()->make(SystemLogServices::class);
        $LogServices->SaveLog($adminInfo->id,'登录系统',$adminInfo->real_name.'登录系统');
        return [
            'token' => $tokenInfo,
            'expires_time' => time(),
            'menus' => $systme_menus['menus'],
            'unique_auth' => $systme_menus['unique_auth'],
            'user_info' => [
                'id' => $adminInfo->id,
                'account' => $adminInfo->account,
                'level'=>$adminInfo->level,
                'real_name'=>$adminInfo->real_name,
                'store_num'=>$adminInfo->store_num,
                'sms_num'=>$adminInfo->sms_num,
                'is_warning'=>$warning,

            ]

        ];

    }


   /**
     * 修改管理员
     * @param int $id
     * @param array $data
     * @return bool
     */
    public function save(int $uid, array $data)
    {



        return $this->dao->updatePass($uid,$data);



    }

    /**
     * 修改当前管理员信息
     * @param int $id
     * @param array $data
     * @return bool
     */
    public function updateAdmin(int $id, array $data)
    {

    }


}
